package com.action.photo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.photo.PhotoDetail;
import com.business.photo.PhotoDetailBus;
import com.config.SystemGlobals;
import com.jspsmart.upload.SmartUpload;
import com.util.MD5;
import com.util.TextTools;

public class ManagePhoto extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
			Log log = LogFactory.getLog(ManagePhoto.class);
		
		if (request.getParameter("deleted") != null) {
			
			String[] photoNos = request.getParameterValues("deleted");
			PhotoDetailBus lib = new PhotoDetailBus();
			for(int i=0;i<photoNos.length;i++){
			lib.del(photoNos[i]);
			}
			out
					.println("<script>alert(\"删除图片成功\");window.location=\"admin/blog/photolist.jsp\";</script>");
			out.close();
		}else{
		SmartUpload SmartUpload = new SmartUpload();
		SmartUpload.initialize(getServletConfig(), request, response);
		SmartUpload.setTotalMaxFileSize(0x4c4b40L);
		try {
			SmartUpload.upload();
		} catch (Exception ex) {
			log.error("smartUpload upload():" + ex.getMessage());
		}
	
		String action= SmartUpload.getRequest().getParameter("action");
		if (action!=null&&action.equals("edit")) {
			
		String startpath = SystemGlobals.getValue("file_upload_path");
		startpath = startpath + "/images";
		String filepath = "";
		try {
			for (int i = 0; i < SmartUpload.getFiles().getCount(); i++) {
				com.jspsmart.upload.File myfile = SmartUpload.getFiles()
						.getFile(i);
				String imagefile = myfile.getFileName();
				if (!myfile.isMissing()) {
					MD5 md5 = new MD5();
					String md5str = md5.getMD5ofStr(myfile.getContentString());
					imagefile = md5str + "." + myfile.getFileExt();
					myfile.saveAs(startpath
							+ TextTools.mkHashDir(startpath, md5str) + "/"
							+ imagefile);
					filepath = "/upload/images"
							+ TextTools.mkHashDir(startpath, md5str) + "/"
							+ imagefile;
				}
			}
		} catch (Exception se) {
			log.error("the upload File" + filepath + " is Failed "
					+ se.toString());
		}
		String photoUrl = filepath;
		String photoName = SmartUpload.getRequest().getParameter("photoname");
		String photoMemo = SmartUpload.getRequest().getParameter("photomemo");
		String username = SmartUpload.getRequest().getParameter("username");
		String phototypeId = SmartUpload.getRequest().getParameter(
				"phototypeid");
		String isShow = SmartUpload.getRequest().getParameter("isshow");
		String replyCount=SmartUpload.getRequest().getParameter("replycount");
		String viewCount= SmartUpload.getRequest().getParameter("viewcount");
		if (isShow == null) {
			isShow = "0";
		}
		PhotoDetail pd = new PhotoDetail();
		pd.setUsername(username);
		pd.setIsShow(isShow);
		pd.setPhotoMemo(photoMemo);
		pd.setPhototypeId(phototypeId);
		pd.setPhotoName(photoName);
		pd.setReplyCount(replyCount);
		pd.setViewCount(viewCount);
		PhotoDetailBus pdb = new PhotoDetailBus();
		String photoNo = SmartUpload.getRequest().getParameter("no");
		String url =  SmartUpload.getRequest().getParameter("photourl");
		if (photoUrl.equals("")) {
			photoUrl = url;
		}
		pd.setPhotoUrl(photoUrl);
		pdb.update(pd, photoNo);
			out
					.println("<script>alert(\"编辑图片信息成功\");window.location=\"admin/blog/photolist.jsp\";</script>");
			out.close();
		}
	}
	}

}
